package org.swingDemo;

import java.io.Console;
import java.sql.SQLOutput;
import java.text.MessageFormat;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTestHarness {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("Enter your regex: ");
            Pattern pattern = Pattern.compile(scanner.next());
            System.out.println("Enter input string to search: ");
            Matcher matcher = pattern.matcher(scanner.next());

            MessageFormat formatter = new MessageFormat("I found the text " +
                    " \"{0}\" starting at " +
                    "index {1} and ending at index {2}.\n");
            boolean found = false;
            while (matcher.find()) {
                Object[] messageArguments = {
                        matcher.group(),
                        matcher.start(),
                        matcher.end()
                };
                System.out.println(formatter.format(messageArguments));
                found = true;
            }
            if (!found) {
                System.out.println("No match found.\n");
            }
        }
    }
}
